Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Notice that each time an image is selected from the list, the image is read from the database. It could be very costly in terms of memory resources to save all of the images, so we’ll just get the image from the database when needed. When the user selects an item from the list, we can get the image description. This description is used to get the icon ID from the image Hashtable. For the most part, we follow the same steps we have seen several times before in getting results from a database. Unfortunately, we’ve had to use a very nasty workaround here. The image is retrieved from the database as a binary InputStream, and it is from this InputStream that we need to draw the image on our canvas. This technique seems like it should be a simple matter, but it turns out to be impossible as of the writing of this book. To get around this problem, the IconStore application uses the InputStream to create a temporary file on disk, from which an image can be loaded and drawn on the canvas. Hopefully, a method to draw images from an InputStream will be part of Java in the future.

Figure 8.3 shows the IconStore screen after the user has selected an image from the initial category list. Figure 8.4 shows the IconStore screen after the user has changed the category (from the Icons menu) to sports and has made a selection.


Figure 8.3  Selecting on image from the category list box.


Figure 8.4  Changing the image category.

Saving The Image

All that’s left is to add the ability to save the image to disk. We saw previously how to handle the Save As menu event, so we just need to be able to create the disk file. Our workaround approach for drawing an image from an InputStream will be used to our advantage. Because an image file has already been created, we can simply make a copy of the temporary file. Listing 8.10 shows the code to copy a file.

Listing 8.10 Copying a file.

//————————————————————————————————————
// copyFile
// Copy the source file to the target file.
//————————————————————————————————————
public boolean copyFile(
    String source,
    String target)
{
    boolean rc = false;

    try {
           FileInputStream in = new FileInputStream(source);
          FileOutputStream out = new FileOutputStream(target);

        int bytes;
       byte b[] = new byte[1024];

        // Read chunks from the input stream and write to the output
        // stream.
        while (true) {
            bytes = in.read(b);
            if (bytes == -1) {
                break;
          }
             out.write(b, 0, bytes);
        }
        in.close();
        out.close();
        rc = true;
   }
     catch (java.lang.Exception ex) {
          ex.printStackTrace();
   }

    return rc;
}

Figure 8.5 shows the IconStore screen after the user has selected the Save As menu option.


Figure 8.5  The IconStore Save As dialog box.

That’s all there is to it.

Summary

Let’s recap the important details that we have covered in this chapter:

  Creating a basic GUI Java application
  Opening a connection to a data source
  Using database data to create dynamic GUI components (menus and lists)
  Handling user events
  Handling JDBC InputStreams

If you would like to take the IconStore application further, one obvious enhancement would be to allow the user to add images to the database. I’ll leave this as an exercise for you.


Previous Table of Contents Next